package assist.domain.service;

import java.util.Set;

import assist.datastore.dao.DAOFactory;
import assist.datastore.dao.EventDAO;
import assist.domain.model.Stats.Row;
import assist.domain.model.StatsQuery.Column;
import assist.domain.model.StatsQuery.RowQuery;
import assist.domain.model.StatsQuery.ShotType;
import assist.domain.model.StatsQuery.StatType;
import assist.domain.model.Team;

public class StatsService
{
    public Row getStatsRow(RowQuery rowQuery)
    {
        Set<Long> players = rowQuery.getPlayerIds();
        Set<String> gameIds = rowQuery.getGameIds();
        Set<Column> columns = rowQuery.getColumns();
        Team team = rowQuery.getTeam();
        Row row = new Row();
        row.setRowQuery(rowQuery);
        for (Column column : columns)
        {
            ShotType shotType = column.getShotType();           
            EventDAO<?> eventDAO = null;
            Double statFor = 0d;
            Double statAgainst = 0d;
            
            if(shotType == ShotType.MISS || shotType == ShotType.CORSI || shotType == ShotType.FENWICK)
            {
                eventDAO = DAOFactory.getDAOFactory().getMissedShotDAO();              
                if (StatType.FOR == column.getStatType() || StatType.RATIO  == column.getStatType())
                {
                    statFor += eventDAO.getEventCount(team, players, gameIds, column.getStrength(), false);  
                }
                if (StatType.AGAINST == column.getStatType() || StatType.RATIO  == column.getStatType())
                {
                    statAgainst += eventDAO.getEventCount(team, players, gameIds, column.getStrength(), true);
                }
            }
            if(shotType == ShotType.BLOCK || shotType == ShotType.CORSI)
            {
                eventDAO = DAOFactory.getDAOFactory().getBlockedShotDAO();
                if (StatType.FOR == column.getStatType() || StatType.RATIO  == column.getStatType())
                {
                    statFor += eventDAO.getEventCount(team, players, gameIds, column.getStrength(), false);  
                }
                if (StatType.AGAINST == column.getStatType() || StatType.RATIO  == column.getStatType())
                {
                    statAgainst += eventDAO.getEventCount(team, players, gameIds, column.getStrength(), true);
                }                   
                                
            }
            if(shotType == ShotType.SOG || shotType == ShotType.CORSI || shotType == ShotType.FENWICK)
            {
                eventDAO = DAOFactory.getDAOFactory().getShotOnGoalDAO();
                if (StatType.FOR == column.getStatType() || StatType.RATIO  == column.getStatType())
                {
                    statFor += eventDAO.getEventCount(team, players, gameIds, column.getStrength(), false);  
                }
                if (StatType.AGAINST == column.getStatType() || StatType.RATIO  == column.getStatType())
                {
                    statAgainst += eventDAO.getEventCount(team, players, gameIds, column.getStrength(), true);
                }                               
            }
            if (StatType.FOR == column.getStatType())
            {
                row.set(column, statFor);
            }
            if (StatType.AGAINST == column.getStatType())
            {
                row.set(column, statAgainst);
            }
            if (StatType.RATIO == column.getStatType())
            { 
                row.set(column, statFor / (statFor + statAgainst));
            }            
        }
        return row;
    }
}
